﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tcbci.Task;
using System.Dynamic;

namespace TC.WEB.Site.Core.Task
{
    public class SMSSendTask:TaskHandler
    {
        public SMSSendTask()
        {
            this.Interval = NoInterval;
        }

        protected override void DoWork()
        {
            //原始短信发送方法
            //string messag  = string.Empty;
            //if (!TC.WEB.Site.Models.Tools.MailHelper.SendMobileMsg(TaskArgs.Message, TaskArgs.Mobile, out messag))
            //{
            //    Ocor.Logger.LogWriter.Write(Ocor.Logger.LogLevel.Warning, "SMSSendTask", messag);
            //}
            try
            {
                var sms = Tcbci.Net.SMSOpertorFactory.SMS;
                Ocor.Logger.LogWriter.Write(Ocor.Logger.LogLevel.Debug, "SMSSendTask", "ss");
                string r = sms.SendSms(TaskArgs.Mobile, TaskArgs.Message);
                Ocor.Logger.LogWriter.Write(Ocor.Logger.LogLevel.Debug, "SMSSendTask", r);
            }
            catch (Exception ex) {
                Ocor.Logger.LogWriter.Write(ex, "SMSSendTask");
            }
        }

        public static void CreateAndRun(string mobile, string message)
        {
            var task = new SMSSendTask();

            dynamic args = new ExpandoObject();

            args.Mobile = mobile;
            args.Message = message;

            task.TaskArgs = args;

            TaskManager.Current.RegisterTaskHandler(task);
        }
    }
}